// pages/table1/table1.js
const network = require('../../../../utils/network.js')
var util = require('../../../../utils/util.js');
var app = getApp();
var pageSize = 0;
var locId;
var drugList=[];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headers: [{ text: 'stuffCode', display: '代码' }, { text: 'stuffName', display: '项目名称' }, { text: 'standard', display: '规格' }, { text: 'unitName', display: '单位' }, { text: 'quantity', display: '库存数量' }, { text: 'buyAmount', display: '购进总金额' }, { text: 'saleAmount', display: '零售总金额' }],
    row: [],
    current: 1,
    totalPage: 1,
    time_now:""
  },
  onChange(e) {
    console.log('onChange', e)
    this.setData({
      current: e.detail.current,
    })
    
    this.myRequest(e)
  },

  myRequest: function (options) {
    var that = this;
    console.log('options:', options)
    //获得表格高度
    var windowheight = wx.getSystemInfoSync().windowHeight;
    var tdHeight = [18.4, 21.6, 39]
    if (windowheight < 550) {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[0]);
    } else if (windowheight < 850) {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[1]);
    } else {
      pageSize = Math.round(windowheight * 0.8 / tdHeight[2]);
    }
      console.log("每页个数", pageSize);
    //调用封装好的网络请求，获取数据
    network.get('/drug/stock?locId=' + locId + '&drugList=' + drugList + '&pageCount=' + that.data.current + '&pageSize=' + pageSize, {}).then(function (res) {
      console.log('res:', res)

      that.setData({
        time_now: util.formatTime(new Date()),
        msg: res.data.errMsg,
        row: res.data.data,
        totalPage: res.data.totalPage
      })
      console.log('row:', that.data.row)

    }).then(function (res) {
      wx.hideLoading()
      //that.createColumnChart();

    }).catch(function (res) {
      console.log(res)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    locId = options.locId
    drugList = options.drugList
    that.myRequest(options)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})